using Circus.CodeEditor;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Circus.CodeEditor.CodeCompletion
{
	[DesignTimeVisible(false), ToolboxItem(false)]
	public class CodeCompletionEdit : CodeCompletionWindow, ICodeCompletionWindow 
	{
		private CodeCompletionBox parent;
		public override bool IsFocused
		{
			get
			{
				return this.Focused || this.Edit.Focused || this.Edit.EditBox.Focused || this.Edit.PathLabel.Focused;
			}
		}
		public virtual string EditField
		{
			get
			{
				return this.Edit.Label.Text;
			}
			set
			{
				if (this.Edit.Label.Text != value)
				{
					this.Edit.Label.Text = value;
					this.Edit.UpdateSize();
				}
			}
		}
		public string EditPath
		{
			get
			{
				return this.Edit.PathLabel.Text;
			}
			set
			{
				if (this.Edit.PathLabel.Text != value)
				{
					this.Edit.PathLabel.Text = value;
					this.OnEditPathChanged();
				}
			}
		}
		public string EditText
		{
			get
			{
				return this.Edit.EditBox.Text;
			}
			set
			{
				if (this.Edit != null && this.Edit.EditBox != null && this.Edit.EditBox.Text != value)
				{
					this.Edit.EditBox.Text = value;
					this.OnEditTextChanged();
				}
			}
		}
		public virtual CompletionEdit Edit
		{
			get
			{
				return this.PopupControl as CompletionEdit;
			}
		}
		protected override Control CreatePopupControl()
		{
			return new CompletionEdit();
		}
		protected override void WndProc(ref Message m)
		{
			int msg = m.Msg;
			if (msg != 133)
			{
				base.WndProc(ref m);
			}
			else
			{
				base.WndProc(ref m);
				if (base.FormBorderStyle == FormBorderStyle.FixedToolWindow && XPThemes.CurrentTheme != XPThemeName.None)
				{
					IntPtr windowDC = OSUtils.GetWindowDC(base.Handle);
					try
					{
						OSUtils.ExcludeClipRect(windowDC, 2, 2, base.Width - 2, base.Height - 2);
						XPThemes.DrawEditBorder(windowDC, new Rectangle(0, 0, base.Width, base.Height));
					}
					finally
					{
						OSUtils.ReleaseDC(base.Handle, windowDC);
					}
				}
			}
		}
		protected virtual void DoTextChanged(object sender, EventArgs e)
		{
			if (this.parent != null)
			{
				this.parent.PerformSearch();
			}
		}
		protected override void DoProcessKeyMessage(ref Message m)
		{
		}
		protected override bool ProcessKeyPreview(ref Message m)
		{
			bool result;
			if (this.OnProcessKeyPreview(ref m))
			{
				result = true;
			}
			else
			{
				if (m.Msg == 256 || m.Msg == 260)
				{
					Keys keys = (Keys)(m.WParam.ToInt32() & 65535);
					Keys keys2 = keys;
					if (keys2 <= Keys.Escape)
					{
						if (keys2 != Keys.Return && keys2 != Keys.Escape)
						{
							goto IL_E8;
						}
					}
					else
					{
						switch (keys2)
						{
						case Keys.Prior:
						case Keys.Next:
							break;
						default:
							switch (keys2)
							{
							case Keys.Up:
							case Keys.Down:
								break;
							case Keys.Right:
								goto IL_E8;
							default:
								goto IL_E8;
							}
							break;
						}
					}
					if (this.parent != null && this.parent.PopupControl.IsHandleCreated)
					{
						OSUtils.SendMessage(this.parent.PopupControl.Handle, m.Msg, m.WParam, m.LParam);
						result = true;
						return result;
					}
					IL_E8:;
				}
				result = base.ProcessKeyPreview(ref m);
			}
			return result;
		}
		protected virtual void OnEditPathChanged()
		{
		}
		protected virtual void OnEditTextChanged()
		{
		}
		public CodeCompletionEdit(CodeEditor owner, CodeCompletionBox parent) : base(owner)
		{
			this.parent = parent;
			base.FormBorderStyle = FormBorderStyle.FixedToolWindow;
			base.Width = 300;
			base.Height = this.Font.Height + 2;
			if (parent != null)
			{
				this.CompletionFlags = parent.CompletionFlags;
			}
			this.CompletionFlags &= ~(CodeCompletionFlags.CloseOnEscape | CodeCompletionFlags.AcceptOnEnter | CodeCompletionFlags.AcceptOnDblClick | CodeCompletionFlags.AcceptOnDelimiter);
			this.CompletionFlags |= CodeCompletionFlags.KeepActive;
			this.Edit.EditBox.TextChanged += new EventHandler(this.DoTextChanged);
		}
		public override bool ContainsControl(Control control)
		{
			return base.ContainsControl(control) || (control != null && (control == this.parent || control == this.parent.PopupControl));
		}
	 
	
	}
}
